//
//  RecHeaderItemCell.swift
//

import UIKit

class RecHeaderItemCell: UICollectionViewCell {
    
    var items: [RecChildModel] = [] {
        didSet {
            collectionView.reloadData()
        }
    }
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let itemWidth = (UIScreen.width - space * 2 - 25.fit * 4) / 5
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth + 25.fit)
        layout.minimumLineSpacing = 25.fit
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.sysTableView
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(RecHeaderItemChildCell.self)
        collectionView.isScrollEnabled = false
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
           
        addSubviews()
    }
       
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSubviews() {
        backgroundColor = .sysTableView
        contentView.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}

extension RecHeaderItemCell: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withClass: RecHeaderItemChildCell.self, for: indexPath)
        cell.iconView.kf_set(items[indexPath.item].viewP.iconUrl)
        cell.titleLabel.text = items[indexPath.item].title
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let currVC = self.currVC else { return }
        let model = items[indexPath.item]
        switch model.layoutType {
        case .vip:
            break
        default:
            let vc = RecVideoMoreViewController()
            vc.navTitle = model.title
            vc.id = model.contentId
            vc.layoutType = model.layoutType
            currVC.push(vc)
        }
    }
}
